﻿@model List<BlogPost>
@{
    Dictionary<int, BlogPostVote> pv = ViewBag.PostsVote;
}

@foreach (var p in Model)
{
<div class="card window-card container-fluid">
<div class="card-body">
<div class="blog-title">
    <h2><a class="text-decoration-none text-reset" asp-area="Blog" asp-action="Entry" asp-controller="Blog" asp-route-postId="@p.Id">@p.Title</a></h2>
    <p class="mb-0">By <user uid="@p.UserId" username="@p.UserDetail.UserName" />, <span title="@p.PublishTime">@Html.AutoUnitTimespan(DateTimeOffset.Now - p.PublishTime) ago</span><razor asp-show-if="p.Revision > 1">, revision @p.Revision</razor>.</p>
</div>
<hr class="blog-line-separator" />
<div class="blog-content">
    @Html.Raw(p.ContentHtml)
</div>
<hr class="blog-line-separator blog-line-separator-end" />
<p class="mb-0">
    <span class="voting" id="blog_vote_@(p.Id)"><partial name="/Areas/Blog/Views/Shared/VotingDetail.cshtml" model="(p.Id, (int?)null, p.UpVotes, p.DownVotes, pv?.GetValueOrDefault(p.Id)?.Up)" /></span>
</p>
</div>
</div>
}

<script type="text/javascript">
    $(function () {
        let $body = $('body');

        $body.on('click', '.voter.enabled', function () {
            notice('Your vote has been sent. It will be processed in a minute.');
            ajaxget($(this).data("href"), '#' + $(this).parent().prop('id'));
            return false;
        });

        $body.on('click', '.voter.disabled', function () {
            notice('You cannot vote twice. You have already voted for this comment before.', 'danger');
            return false;
        });
    });
</script>
